package com.example.demo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-16
 * Time: 20:53
 */
@RestController
public class UserController {

    //登陆方法
    @RequestMapping("/login")
    public boolean login(HttpServletRequest req,
                      String username,String password) {
        if(StringUtils.hasLength(username)&&
        StringUtils.hasLength(password)) {//验证用户名,密码是否有效
            //验证用户名和密码是否正确
            if(username.equals("admin")&&
            password.equals("admin")) {
                //用户名和密码正确,存到session里面
                HttpSession session = req.getSession();
                session.setAttribute("userinfo","admin");
                return true;
            }
        }
        return false;
    }

    //获取用户信息的方法
    @RequestMapping("/userinfo")
    public Object gerUserInfo() {
        return "userinfo";
    }

    //获取注册信息
    @RequestMapping("/reg")
    public Object getReg() {
        return "reg";
    }
}